//index.js

Page({
  data: {
    item:0,
    tab:0,
    playlist: [{
      id: 1,title: '所念皆星河',singer: '房东的猫',
      src: 'http://music.163.com/song/media/outer/url?id=1476239407.mp3',coverImgUrl: 'http://p3.music.126.net/hh8cQ-Zd5SxVH4PzguGpYQ==/109951165342791905.jpg?param=100y100'
    }, {
      id: 2,title: '我可以',singer: '房东的猫',
      src: 'http://music.163.com/song/media/outer/url?id=1401909095.mp3',coverImgUrl: 'http://p4.music.126.net/kkzDE_dbREIBZtRfrFJeHw==/109951165359522872.jpg?param=100y100'
    }, {
      id: 3,title: '得不到你',singer: '隔壁老樊',
      src: 'http://music.163.com/song/media/outer/url?id=1373236574.mp3',coverImgUrl: 'http://p4.music.126.net/CFGqXK_z6QSo4rEBXcJn_Q==/109951165334747710.jpg?param=100y100'
    }, {
      id: 4,title: '这一生关于你的风景',singer: '隔壁老樊',
      src: 'http://music.163.com/song/media/outer/url?id=1383927243.mp3',coverImgUrl: 'http://p3.music.126.net/q6h3zFrzr6Y3NlQT8rir6w==/109951165373635125.jpg?param=100y100'
    }],
    state: 'paused',
    playIndex: 0,
    play: {
      currentTime: '',
      duration: '',
      percent: 0,
      title: '',
      singer: '',
      coverImgUrl: 'http://p3.music.126.net/hh8cQ-Zd5SxVH4PzguGpYQ==/109951165342791905.jpg?param=100y100',
    }
  },
  changeItem:function(e){
    // const item = e.target.dataset.item;
    this.setData({
      // tab:item,
      item:e.target.dataset.item
    })
  },
  //事件处理函数
  changeTab: function(e){
    this.setData({
      tab:e.detail.current
    })
  },

  // 音乐播放
setMusic: function(index) {
  var music = this.data.playlist[index]
  this.audioCtx.src = music.src
  this.setData({
    playIndex: index,
      'play.title': music.title,
      'play.singer': music.singer,
      'play.coverImgUrl': music.coverImgUrl,
      'play.currentTime': '00:00',
      'play.duration':  "00:00",
      'play.percent': 0
    })
    
  },
 // 实现播放器播放功能
 audioCtx: null,
 onReady: function() {
   this.audioCtx = wx.createInnerAudioContext()
   // 默认选择第1曲
   this.setMusic(0)
   var that = this
   // 播放进度检测
   this.audioCtx.onError(function() {
     console.log('播放失败：' + that.audioCtx.src)
   })
   // 播放完成自动换下一曲
   this.audioCtx.onEnded(function() {
     that.next()
   })
   // 自动更新播放进度
   this.audioCtx.onPlay(function() {})   
    this.audioCtx.onTimeUpdate(function() { 
      // console.log(this.formatTime(this.audioCtx.duration))  //获取音乐状态
      that.setData({
        'play.duration': that.formatTime(that.audioCtx.duration),  //当前音频长度
        'play.currentTime': that.formatTime(that.audioCtx.currentTime),  //当前音频播放位置   调用下方formatTime函数
        'play.percent': that.audioCtx.currentTime / that.audioCtx.duration * 100     //百分比
      })
    })
 },

  // 格式化时间
 formatTime:function (time) {
  var minute = Math.floor(time / 60) % 60;
  var second = Math.floor(time) % 60   //Math.floor向下取整数
  return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
},
 
  // 播放按钮
  play: function() {
    this.audioCtx.play()
    this.setData({
      state: 'running'
    })
  },

  // 暂停按钮
  pause: function() {
    this.audioCtx.pause()
    this.setData({
      state: 'paused'
    })
  },

  // 下一曲按钮
  next: function() {
    var index = this.data.playIndex >= this.data.playlist.length - 1 ? 0 : this.data.playIndex + 1
    this.setMusic(index)
    if (this.data.state === 'running') {
      this.play();
    }
  },

   // 滚动条调节歌曲进度
   sliderChange: function(e) {
    var second = e.detail.value * this.audioCtx.duration / 100
    this.audioCtx.seek(second)
  },

   //播放列表换曲功能
  change: function(e) {
    this.setMusic(e.currentTarget.dataset.index)
    this.play()
  }
})
